package littlewhisker.components.ui.button {
	import flash.events.MouseEvent;

	import littlewhisker.components.ui.AbstractUIComponent;

	import flash.events.Event;

	/**
	 * @author Philip
	 */
	public class AbstractButton extends AbstractUIComponent implements IButtonComponent {
		
		private var _event : Event;
		private var _callback : Function;
		private var _callbackArguments : Array;
		
		public function get event() : Event {
			return _event;	
		}
			
		public function set event( event:Event ):void {
			_event = event;	
		}
	
		
		public function get callback() : Function {
			return _callback;
		}
		
		public function set callback(callback : Function) : void {
			_callback = callback;
		}
		
		public function get callbackArguments() : Array {
			return _callbackArguments;
		}
		
		public function set callbackArguments(callbackArguments : Array) : void {
			_callbackArguments = callbackArguments;
		}
	
		override protected function enable():void {
			asset.useHandCursor 	= true;
			asset.mouseEnabled 		= true;
			asset.buttonMode 		= true;		
			//asset.mouseChildren 	= false;	
			asset.addEventListener( MouseEvent.CLICK, onClick );
			asset.addEventListener( MouseEvent.ROLL_OVER, onRollOver );
			asset.addEventListener( MouseEvent.ROLL_OUT, onRollOut );
			asset.addEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );	
			asset.addEventListener( MouseEvent.MOUSE_DOWN, onDown );	
			asset.addEventListener( MouseEvent.MOUSE_UP, onUp );
			asset.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			asset.addEventListener(Event.REMOVED, onRemoved);
		}
		
		protected function onRemoved(event:Event):void{
			asset.removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			asset.removeEventListener(Event.REMOVED, onRemoved);
			disable(); 
		}

		override protected function disable():void {
			asset.useHandCursor 	= false;
			asset.mouseEnabled 		= false;
			asset.buttonMode 		= false;	
			//asset.mouseChildren 	= true;	
			asset.removeEventListener( MouseEvent.CLICK, onClick );
			asset.removeEventListener( MouseEvent.ROLL_OVER, onRollOver );
			asset.removeEventListener( MouseEvent.ROLL_OUT, onRollOut );
			asset.removeEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );	
			asset.removeEventListener( MouseEvent.MOUSE_DOWN, onDown );
			asset.removeEventListener( MouseEvent.MOUSE_UP, onUp );
		}
		
		protected function onClick(e:MouseEvent):void{
			if(event) eventDispatcher.dispatchEvent(event);	
			//GM: test
			if(callback != null) {
				var func:Function = callback;
				func.apply(false,callbackArguments);
			}
		}
		
		protected function onUp(e:MouseEvent):void{
			
		}
		
		protected function onDown(e:MouseEvent):void{
			
		}
		
		protected function onRollOver(e:MouseEvent):void{
		}
		
		protected function onRollOut(e:MouseEvent):void{
		}
		
		protected function onDoubleClick(e:MouseEvent):void {
		}
		
	
	}
}
